package com.wifi.channel.servlet;

import com.alibaba.fastjson.JSON;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;
import com.wifi.channel.entity.Channel;
import com.wifi.channel.service.ChannelService;
import com.wifi.channel.service.ChannelServiceImpl;
import com.wifi.unti.LoginUtil;
import com.wifi.user.entity.Menu;
import com.wifi.user.service.MenuService;
import com.wifi.user.service.MenuServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/channel/*")
public class ChannelManServlet extends BaseServlet {

    LoginUtil loginUtil = new LoginUtil();

    ChannelService channelService=new ChannelServiceImpl();

    @ShenZhouWifiMethod("/del")
    public void channelDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
//        String vlid = request.getParameter("vlid");
        System.out.println(id);
//        System.out.println(vlid);
        if (id.isEmpty()){response.getWriter().write("2");}else {
        Object o=channelService.getchannelByid(Integer.valueOf(id));
        if (o==null){
            response.getWriter().write("2");
        }else{
            int status=((Channel)o).getIsDel();
            int i=0;
            if (status==0){
                 i=channelService.setDel(Integer.valueOf(id),1);
            }else {
                i=channelService.setDel(Integer.valueOf(id),0);
            }
            if (i>0){response.getWriter().write("1");
            }else {response.getWriter().write("3");}
        }
        }
        }


    @ShenZhouWifiMethod("/class1")
    public void class1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Object> channelClass1=channelService.getchannelClass1();
        System.out.println(JSON.toJSONString(channelClass1));
        response.getWriter().write(JSON.toJSONString(channelClass1));
    }

    @ShenZhouWifiMethod("/class2")
    public void class2(HttpServletRequest request, HttpServletResponse response) throws IOException {
String pid=request.getParameter("class1id");
        System.out.println("class2");
        List<Object> channelClass2=channelService.getchannelClass2(pid);
        System.out.println(JSON.toJSONString(channelClass2));
        response.getWriter().write(JSON.toJSONString(channelClass2));
    }



    @ShenZhouWifiMethod("/add")
public  void addMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name=request.getParameter("name");
        String link=request.getParameter("link");
        String showName=request.getParameter("icon");
        String sirez=request.getParameter("sire");
        String class1=request.getParameter("role");
        String class2=request.getParameter("role2");
        String active=request.getParameter("active");


        if (name.isEmpty()||showName.isEmpty()||sirez.isEmpty()){
            response.getWriter().write("3");}
        else {
            Object o=channelService.ConfirmChannel(name,Integer.valueOf(class1),Integer.valueOf(class2));
            if (o==null){
                Channel channel=new Channel(null,name,Integer.valueOf(class1),Integer.valueOf(class2),link,showName,sirez,null,Integer.valueOf(active),null,null);
                Long i=channelService.add(channel);
          if (i>0){response.getWriter().write("1");}else {response.getWriter().write("2");}
            }
        else{
            response.getWriter().write("4");
        }

    }}

    @ShenZhouWifiMethod("/queryById")
    public  void queryMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String id = request.getParameter("id");
        Object o=channelService.getchannelByid(Integer.valueOf(id));
if (o==null){response.getWriter().write("1");}
else {response.getWriter().write(JSON.toJSONString(o));}
    }

    @ShenZhouWifiMethod("/editUrl")
    public  void editUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id=request.getParameter("ide");
        String link=request.getParameter("linke");
        if (id.isEmpty()){response.getWriter().write("4");}
        else if (link.isEmpty()){
            response.getWriter().write("3");
        }else{
                Integer i=channelService.editUrl(link,id);
                if (i>0){
                    response.getWriter().write("1");
                }else{   response.getWriter().write("2");
                }
            }
    }




}







